package com.cpkso.buss.backend.user.web;

import javax.servlet.http.HttpSession;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.cpkso.buss.backend.user.domain.User;
import com.cpkso.buss.backend.user.service.UserService;
import com.runze.base.web.BaseController;
import com.runze.base.web.Response;

/**
 * 用于
 * @author wrzhxy@qq.com
 * @date 2018年4月2日
 */
@RestController
@RequestMapping("/user")
public class UserController extends BaseController {
	
	/**
	 * 修改账户 - 管理员
	 */
	@RequiresPermissions("user:updateall")
	@PutMapping("/accountM")
	public Response modifyAccount(@ModelAttribute User user) {
		userService.modify(user);
		return success();
	}
	/**
	 * 修改账户 - 用户
	 */
	@RequiresPermissions("user:update")
	@PutMapping("/accountP")
	public Response modifyAccountP(@ModelAttribute User user, HttpSession session) {
		Long id = (Long) session.getAttribute("user");
		userService.modify(user, id);
		return success();
	}
	/**
	 * 审核
	 */
	@RequiresPermissions("user:updateall")
	@PutMapping("/verify")
	public Response verify(@RequestParam Long userId) {
		return userService.verify(userId);
	}
	/**
	 * 重置密码
	 */
	@RequiresPermissions("user:updateall")
	@PutMapping("/resetPwd")
	public Response resetPwd(@RequestParam Long userId) {
		userService.resetPassword(userId);
		return success();
	}
	/**
	 * 升权
	 * @param userId
	 * @return
	 */
	@RequiresPermissions("user:updateall")
	@PutMapping("/up")
	public Response promote(@RequestParam Long userId) {
		userService.promote(userId);
		return success();
	}
	/**
	 * 降权
	 */
	@RequiresPermissions("user:updateall")
	@PutMapping("/down")
	public Response demotion(@RequestParam Long userId) {
		userService.demotion(userId);
		return success();
	}
	/**
	 * 显示所有用户
	 */
	@RequiresPermissions("user:queryall")
	@GetMapping("/all")
	public Response all() {
		return userService.findAll();
	}
	
	@Autowired
	private UserService userService;
}
